import { defineStore } from "pinia";
import { ref } from "vue";
import { getListForUser } from "../apis/menu/menu";
import type { Menu, MenuTree } from "../apis/menu/type";
import { buildTree } from "../utils/treeUtil";

export const useMenuStore = defineStore("menu", () => {

    //左侧菜单
    const leftMenus = ref<MenuTree[]>([]);
    //路由页面
    const routerMenus = ref<MenuTree[]>([]);

    /**
     * 获取用户权限
     */
    const getMenus = async () => {


        if (leftMenus.value.length > 0) return;

        console.log("调接口");

        const res = await getListForUser();

        console.log("aaaa", res);

        let _leftMenus = res.list.filter((item) => item.isButton == 0);
        leftMenus.value = buildTree(_leftMenus).filter(item => item.children.length > 0);

        let _routerMenus = res.list.filter((item) => item.isButton == 0 || item.pathName);
        routerMenus.value = buildTree(_routerMenus);

        
    }

    return { leftMenus, routerMenus, getMenus }


}
    , {
        persist: {
            storage: localStorage,
        }
    }
)